/*--------------------------------------------------------------
 * Modification History
 *--------------------------------------------------------------
 * CRLOG	: CR1528
 * Fix		: User Profile Modificaitons 
 *			: - Add Company Code / Add Method to set/get UserMultiRole
 * By		: PH, HPMS
 * Date		: 02-Dec-2005
 *--------------------------------------------------------------
 */
// Copyright (c) 2002 HTHK

package admin.system.user;

import com.ffpc.sql.*;
import com.system.userMultiRole.UserMultiRoleBean;
import com.system.userMultiRole.UserMultiRoleProcess;

import java.sql.*;

import admin.system.user.User;

import java.util.Vector;

/**
 * A Class class.
 * <P>
 * @author HTHK
 */
public class UserProcess
{
    public static UserBean createBean()
    {
        return new User();
    }

    public static int insert(Connection con, UserBean userBean)
      throws DataException
    {
        User user = (User)userBean;
        user.setConnection(con);
        return user.insert();
    }

    public static int update(Connection con, UserBean userBean)
      throws DataException
    {
        User user = (User)userBean;
        user.setConnection(con);
        return user.update();
    }

    public static int delete(Connection con, UserBean userBean)
      throws DataException
    {
        User user = (User)userBean;
        user.setConnection(con);
        return user.delete();
    }

    public static UserBean[] find(Connection con, UserBean userBean)
      throws DataException
    {
        User user = (User)userBean;
        BaseSqlCommand buc = new BaseSqlCommand(con);
        Vector rows = buc.find(user);
        UserBean[] beans = new User[rows.size()];
        for (int i=0;i<rows.size();i++)
        {
            User bean = new User();
            bean.setColumns((Column[])rows.get(i));
			/* CR1528 */
			UserMultiRoleBean userMultiRole = UserMultiRoleProcess.createBean();
			userMultiRole.setUser_id( bean.getUser_id() );
			UserMultiRoleBean[] userMultiRoles = UserMultiRoleProcess.find(con,userMultiRole);

			Vector roles = new Vector();
			for( int idx=0; idx < userMultiRoles.length; idx++ ) {
				roles.addElement( userMultiRoles[idx].getRole_id() );
			}

			bean.setRoles(roles);
			/* CR1528 */
            beans[i] = bean;
        }
        return beans;
    }
}


